<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>checkProp</title>
</head>

<body>
    <div>打开浏览器控制台查看结果</div>
    <script>
        const checkProp = (predicate, prop) => obj => !!predicate(obj[prop]);
        const lengthIs4 = checkProp(l => l === 4, 'length');
        console.log('1', lengthIs4([])); // false
        console.log('1', lengthIs4([1, 2, 3, 4])); // true
        console.log('1', lengthIs4(new Set([1, 2, 3, 4]))); // false (Set uses Size, not length)

        const session = { user: {} };
        const validUserSession = checkProp(u => u.active && !u.disabled, 'user');

        console.log('2', validUserSession(session)); // false

        session.user.active = true;
        console.log('2', validUserSession(session)); // true

        const noLength = checkProp(l => l === undefined, 'length');
        console.log('3', noLength([])); // false
        console.log('3', noLength({})); // true
        console.log('3', noLength(new Set())); // true
    </script>
</body>

</html>